$(function() {


    var Map = function () {
        this._data = [];
        this.set = function (key, val) {
            for (var i in this._data) {
                if (this._data[i].key == key) {
                    this._data[i].val = val;
                    return true;
                }
            }
            this._data.push({
                key: key,
                val: val,
            });
            return true;
        };
        this.get = function (key) {
            for (var i in this._data) {
                if (this._data[i].key == key)
                    return this._data[i].val;
            }
            return null;
        };
        this.delete = function (key) {
            for (var i in this._data) {
                if (this._data[i].key == key) {
                    this._data.splice(i, 1);
                }
            }
            return true;
        };
    };

    var map = new Map();

    $(document).on("click", ".add-attr-btn", function () {
        var name = $(".add-attr-input").val();
        name = $.trim(name);
        if (name == "")
            return;
        page.skuAttrs.push({
            attr_name: name,
            sku_list: [],
        });

        $(".add-attr-input").val("");
        page.checkAttrs = getAttrList();
    });

    $(document).on("click", ".add-attr-btn", function () {
        var name = $(this).parents(".attr-list-group").find(".add-attr-input").val();
        var index = $(this).attr("index");
        name = $.trim(name);
        if (name == "")
            return;
        page.skuAttrs[index].sku_list.push({
            spec_name: name,
        });
        $(this).parents(".attr-list-group").find(".add-attr-input").val("");
        page.checkAttrs = getAttrList();
    });

    $(document).on("click", ".attr-group-delete", function () {
        var index = $(this).attr("index");
        page.skuAttrs.splice(index, 1);
        page.checkAttrs = getAttrList();
    });

    $(document).on("click", ".attr-delete", function () {
        var index = $(this).attr("index");
        var group_index = $(this).attr("group-index");
        page.skuAttrs[group_index].sku_list.splice(index, 1);
        page.checkAttrs = getAttrList();
    });

    function getAttrList() {
        var array = [];
        for (var i in page.skuAttrs) {
            for (var j in page.skuAttrs[i].sku_list) {
                var object = {
                    attr_id: null,
                    attr_name: page.skuAttrs[i].attr_name,
                    spec_id: null,
                    spec_name: page.skuAttrs[i].sku_list[j].spec_name,
                };
                if (!array[i])
                    array[i] = [];
                array[i].push(object);
            }
            //console.log(array);
        }
        //console.log(array);
        var len = array.length;
        var results = [];
        var indexs = {};

        function specialSort(start) {
            start++;
            if (start > len - 1) {
                return;
            }
            if (!indexs[start]) {
                indexs[start] = 0;
            }
            if (!(array[start] instanceof Array)) {
                array[start] = [array[start]];
            }
            for (indexs[start] = 0; indexs[start] < array[start].length; indexs[start]++) {
                specialSort(start);
                if (start == len - 1) {
                    var temp = [];
                    for (var i = len - 1; i >= 0; i--) {
                        if (!(array[start - i] instanceof Array)) {
                            array[start - i] = [array[start - i]];
                        }
                        if (array[start - i][indexs[start - i]]) {
                            temp.push(array[start - i][indexs[start - i]]);
                        }
                    }
                    var key = [];
                    for (var i in temp) {
                        key.push(temp[i].attr_id);
                    }
                    var oldVal = map.get(key.sort().toString());
                    if (oldVal) {
                        results.push({
                            id: oldVal.id,
                            stock: oldVal.stock,
                            price: oldVal.price,
                            origin: oldVal.origin,
                            settlement: oldVal.settlement,
                            sku_list: temp,
                        });
                    } else {
                        var obj = {
                            id: 0,
                            stock: 0,
                            price: 0,
                            origin: 0,
                            settlement: 0,
                            sku_list: temp,
                        }

                        results.push(obj);
                    }
                }
            }
        }

        specialSort(-1);
        return results;
    }

});